﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLK.Web.App_Code;
using QLK.Database.BLLs;
using QLK.Database;

namespace QLK.Web
{
    public partial class Employees : System.Web.UI.Page
    {
        private string _EmpID;
        private Employee Emps;
        private EmployeeBLL EmpBLL = new EmployeeBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                Load_Data();
        }

        private void Load_Data()
        {
            _EmpID = Request.QueryString[Columns.EmployeeID];
            if (string.IsNullOrEmpty(_EmpID))
            {
                txtLastName.Text = null;
                txtLastName.Focus();
                txtFirstName.Text = null;
                txtIDCard.Text = null;
                txtPhone.Text = null;
                rdoGender.SelectedIndex = 0;
            }
            else
            {
                txtLastName.Text = Request.QueryString[Columns.LastName];
                txtFirstName.Text = Request.QueryString[Columns.FirstName];
                txtIDCard.Text = Request.QueryString[Columns.IDCard];
                txtPhone.Text = Request.QueryString[Columns.PhoneNumber];
                string Gender = Request.QueryString[Columns.Gender];
                if (Gender.Equals("True"))
                    rdoGender.SelectedIndex = 1;
                else
                    rdoGender.SelectedIndex = 0;
            }
        }

        protected void btnViewEmp_Click(object sender, EventArgs e)
        {
            Response.Redirect(PageUrls.EmployeeDetails);
        }

        protected void btnSaveEmp_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_EmpID))
            {
                Emps = new Employee();
                Emps.LastName = txtLastName.Text;
                Emps.FirstName = txtFirstName.Text;
                Emps.IDCard = txtIDCard.Text;
                Emps.PhoneNumber = txtPhone.Text;
                if (rdoGender.SelectedIndex == 0)
                    Emps.Gender = false;
                else
                    Emps.Gender = true;

                if (EmpBLL.EmployeeInsert(Emps))
                {
                    lblWarning.Text = "Insert employee successfully!";
                    Load_Data();
                }
                else
                {
                    lblWarning.Text = "Insert employee failed!";
                    Load_Data();
                }
            }
            else
            {
                Emps = new Employee();
                Emps.EmployeeID = int.Parse(_EmpID);
                Emps.LastName = txtLastName.Text;
                Emps.FirstName = txtFirstName.Text;
                Emps.IDCard = txtIDCard.Text;
                Emps.PhoneNumber = txtPhone.Text;
                if (rdoGender.SelectedIndex == 0)
                    Emps.Gender = false;
                else
                    Emps.Gender = true;

                if (EmpBLL.EmployeeUpdate(Emps))
                {
                    lblWarning.Text = "Update employee successfully!";
                    Load_Data();
                }
                else
                {
                    lblWarning.Text = "Update employee failed!";
                    Load_Data();
                }
            }
        }
    }
}
